# Maintainer:
# Contributor: netcrusher < tobias AT miglix DOT eu >
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Simo Leone <neotuli@gmail.com>

pkgname=festival
pkgver=2.5.0
pkgrel=4
pkgdesc="A general multi-lingual speech synthesis system"
arch=(x86_64)
url="http://festvox.org/festival/"
license=(BSD GPL custom)
depends=(perl alsa-lib)
options=('!makeflags')
source=(http://festvox.org/packed/festival/${pkgver%.*}/festival-$pkgver-release.tar.gz
        http://festvox.org/packed/festival/${pkgver%.*}/speech_tools-$pkgver-release.tar.gz
        http://festvox.org/packed/festival/${pkgver%.*}/festlex_CMU.tar.gz
        http://festvox.org/packed/festival/${pkgver%.*}/festlex_OALD.tar.gz
        http://festvox.org/packed/festival/${pkgver%.*}/festlex_POSLEX.tar.gz
        speechconfig.patch
        festconfig.patch
        festival-shared-build.patch
        festival-2.5.0-compile.patch)
sha512sums=('529ec3a1780a861074b6070567f3e11cfcc839081f61e078978a491f11eb424bc4d6adf599f073d5571e20f66871b7045df9f9b175a1083d10e8bb02342639b1'
            '013540a5252efb57633f442143a51bc87aac719dfb46c8f47e750322d19dac9b504f55125462a2ee74a6b0bd2661a63a1fc73a8c3c6d2886e38b136a80447fd7'
            'c293344020fd3337193303cd5dfb7e2b0a1012770a484d8bccaae921b0c61b75699204bec45fee0db754506d6f1558fd616866cc94e76afcbb85a4dcbcf9542b'
            'dc7ec5db71ef331f4c4460a741392d548e037369d47080dc734e912aa312bd5c11e66cdcb94bdc4746a3f6c16c0e0294ce159f07b46e350b85c0afab07cc2064'
            'd7f96621fd98bcd3c84f0188fc6ac5a006e2cbb535279693be3341d51148344360f68e67962f08c4041df1e3527de9b30705b13fb9859ad91e3c804704eb5348'
            'd48f899826914a94efcf4005d0e029cebd577ad2c4feb3e1af2e094259e9ca5a5829a6b5c08f22900049c6032ade926af159200d55613cc8d4f3d17e579edd3a'
            'a3d92ae83bf4b55e9bc83469b85eb1540675c3ed06b9bffa8f99a8a0c4fbdb0cb3fc1abe8c8a4fa5b2c22c77d84bbca36b6b2db3ae0e5ef2d7ddfa214a049372'
            'cd14a06e7eaef8755be08635a6354380c0cd67e533d1ec908413f4096b271c5594c054499d016ec4d255180288a68b55d24b891e06e5d04595e3004f2bb62d49'
            '1e00f531710eb7bb4d7fcfd48f53a485023d37a068cfc87323df1fcfd8c358ca3dce21e261dfc73c5a9d724da64f60ea4fca46656215e1cc5055d4f038110567')

prepare() {
  patch -Np0 -i speechconfig.patch
  patch -Np0 -i festconfig.patch

  # build shared libs - taken from Mageia
  patch -Np0 -i festival-shared-build.patch

  patch -p0 -i festival-2.5.0-compile.patch # Fix build (OpenMandriva)

  # Avoid make failure on making scripts and docs
  sed -i "s|examples bin doc|examples|" festival/Makefile
}

build() {
  CFLAGS+=' -fcommon' # https://wiki.gentoo.org/wiki/Gcc_10_porting_notes/fno_common

  # Build Speech Tools first
  cd speech_tools
  ./configure --prefix=/usr --sysconfdir=/etc
  make OPTIMISE_CXXFLAGS="${CXXFLAGS}" OPTIMISE_CCFLAGS="${CFLAGS}"

  # Build Festival itself
  cd ../festival
  ./configure --prefix=/usr --sysconfdir=/etc
  make OPTIMISE_CXXFLAGS="${CXXFLAGS}" OPTIMISE_CCFLAGS="${CFLAGS}"
}

package() {
  # Install Speech Tools first
  cd speech_tools
  install -dm755 "$pkgdir"/usr/{bin,lib,include/speech_tools/{,instantiate,ling_class,rxp,sigpr,unix}}

  #binaries
  #move binaries over wrappers (FS#7864)
  for i in $(grep -l 'EST shared script' bin/*); do
    cp -f main/$(basename $i) bin;
  done
  install -m755 -t "$pkgdir"/usr/bin bin/[a-z]*
  rm -f "$pkgdir"/usr/bin/{est_gdb,est_examples,est_program}

  #libraries
  install -m755 -t "$pkgdir"/usr/lib lib/lib*.so.*
  ln -sf libestbase.so.${pkgver}.1 "$pkgdir"/usr/lib/libestbase.so
  ln -sf libestools.so.${pkgver}.1 "$pkgdir"/usr/lib/libestools.so
  ln -sf libeststring.so.1.2 "$pkgdir"/usr/lib/libeststring.so

  #headers
  for dir in {.,instantiate,ling_class,rxp,sigpr,unix}; do
    install -m644 -t "$pkgdir"/usr/include/speech_tools/$dir include/$dir/*.h
  done

  # Install Festival itself
  cd "$srcdir"/festival

  #binaries
  install -m755 src/main/festival "$pkgdir"/usr/bin/
  install -m755 src/main/festival_client "$pkgdir"/usr/bin/
  install -m755 examples/benchmark "$pkgdir"/usr/bin/
  install -m755 examples/dumpfeats "$pkgdir"/usr/bin/
  install -m755 examples/durmeanstd "$pkgdir"/usr/bin/
  install -m755 examples/latest "$pkgdir"/usr/bin/
  install -m755 examples/make_utts "$pkgdir"/usr/bin/
  install -m755 examples/powmeanstd "$pkgdir"/usr/bin/
  install -m755 examples/run-festival-script "$pkgdir"/usr/bin/
  install -m755 examples/saytime "$pkgdir"/usr/bin/
  install -m755 examples/scfg_parse_text "$pkgdir"/usr/bin/
  install -m755 examples/text2pos "$pkgdir"/usr/bin/
  install -m755 examples/text2wave "$pkgdir"/usr/bin
  install -m755 examples/text2utt "$pkgdir"/usr/bin

  #libraries
  install -m755 src/lib/libFestival.so.* "$pkgdir"/usr/lib/
  ln -sf libFestival.so.2.5.0.0 "$pkgdir"/usr/lib/libFestival.so

  #headers
  install -dm755 "$pkgdir"/usr/include/festival
  install -m644 -t "$pkgdir"/usr/include/festival src/include/*.h

  mkdir -p "$pkgdir"/usr/share/festival
  cp -aR lib/* "$pkgdir"/usr/share/festival
  rm -fv $(find "$pkgdir"/usr/share/festival -name Makefile)
  rm -fv $(find "$pkgdir"/usr/bin -name Makefile)

  #create voices directory
  install -dm755 "$pkgdir"/usr/share/festival/voices

  #licenses
  install -D -m644 "$srcdir"/festival/COPYING \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
  install -D -m644 "$srcdir"/speech_tools/README.md \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE.other

  # Ok now some general cleanups
  for i in $(find "$pkgdir"/usr/include/ -type f); do
    sed -i -e 's,"EST.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/\",speech_tools/,g' \
      -e 's,"siod.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/\",speech_tools/,g' \
      -e 's,"instantiate/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/instantiate/\",speech_tools/instantiate/,g' -e 's,"instantiate,instantiate,g' \
      -e 's,"ling_class/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/ling_class/\",speech_tools/ling_class/,g' -e 's,"ling_class,ling_class,g' \
      -e 's,"rxp/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/rxp/\",speech_tools/rxp/,g' -e 's,"rxp,rxp,g' \
      -e 's,"sigpr/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/sigpr/\",speech_tools/sigpr/,g' -e 's,"sigpr,sigpr,g' \
      -e 's,"unix/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/unix/\",speech_tools/unix/,g' -e 's,\.h\">,.h\>,g' -e 's,"unix,unix,g' \
      -e 's,"festival\.h",\<festival/festival.h\>,g' \
      -e 's,"ModuleDescription\.h",\<festival/ModuleDescription.h\>,g' \
      -e 's,"Phone\.h",\<festival/Phone.h\>,g' $i
  done
}
